package com.foreveross.proxyip.application.impl.pools;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.foreveross.proxyip.application.pool.ApplyIpLogApplication;
import com.foreveross.proxyip.application.pool.vo.ApplyIpLogEntityVo;
import com.foreveross.proxyip.application.pool.vo.ApplyIpLogVo;
import com.foreveross.proxyip.entity.ApplyIpLogEntity;
import com.foreveross.proxyip.infra.PageData;
import com.foreveross.proxyip.infra.base.ddd.repository.IBaseRepository;
@Service
public class ApplyIpLogApplicationImpl implements ApplyIpLogApplication {
	@Resource(name = "entityHibernateRepository")
	IBaseRepository baseRepository;

	/**
	 * 分页获取页面数据
	 * 
	 * @param pageData
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public PageData searchByConditions(ApplyIpLogVo vo, int pageIndex,
			int pageLen) throws Exception {
		
		StringBuffer queryStr = new StringBuffer( "from ApplyIpLogEntity t where 1=1 ");
		
		Object[] finalResult = this.generateHqlByConditions(vo);
		queryStr.append(finalResult[0].toString());
		queryStr.append(" order by t.createTime desc");
		Object [] params = (Object [])finalResult[1];
		List<ApplyIpLogEntity> list = baseRepository.queryResult(queryStr.toString(),
				params, pageIndex, pageLen);
		list = list==null?new ArrayList<ApplyIpLogEntity>():list;
		List result = new ArrayList();
		for(ApplyIpLogEntity log : list){
			result.add(new ApplyIpLogEntityVo(log));
		}
		long size = this.searchSizeByConditions(vo);
		PageData pageData = new PageData();
		pageData.setDataSize(size);
		pageData.setPageIndex(pageIndex);
		pageData.setPageLen(pageLen);
		pageData.setResult(result);
		int pageCount=(int)size/pageLen+((int)size%pageLen>0?1:0);
		pageData.setPageCount(pageCount);
		return pageData;
	}
	
	private long searchSizeByConditions(ApplyIpLogVo vo)throws Exception{
		StringBuffer hql=new StringBuffer("select count(t) from ApplyIpLogEntity t where 1=1");
		Object[] finalResult=generateHqlByConditions(vo);
		hql=hql.append(finalResult[0].toString());
		Object [] params=(Object [])finalResult[1];
		return baseRepository.queryResultSize(hql.toString(), params);
	}
	
	
	private Object[] generateHqlByConditions(ApplyIpLogVo vo){
		Object[] finalResult=new Object[2];
		

		List<Object> params = new ArrayList<Object>();
		
		ApplyIpLogEntity applyLog = vo.getApplyLog();

		StringBuffer queryStr =  new StringBuffer();
		if (StringUtils.isNotBlank(applyLog.getChannelCode())) {
			queryStr.append(" and t.channelCode=?");
			params.add(applyLog.getChannelCode());
		}
		if (StringUtils.isNotBlank(applyLog.getIps())) {
			queryStr.append(" and t.ips like %?%");
			params.add(applyLog.getIps());
		}
		if (applyLog.getState()!=null) {
			queryStr.append(" and t.state=?");
			params.add(applyLog.getState());
		}
		if (vo.getBeginTime()!=null) {
			queryStr.append(" and t.createTime>=?");
			params.add(vo.getBeginTime());
		}
		if (vo.getEndTime()!=null) {
			queryStr.append(" and t.createTime<=?");
			params.add(vo.getEndTime());
		}
		finalResult[0] = queryStr;
		finalResult[1] = params.toArray();
		return finalResult;
	}

}
